package com.guli.ucenter.controller;


import com.guli.commonUtils.JwtUtils;
import com.guli.commonUtils.R;
import com.guli.commonUtils.vo.UcenterVO;
import com.guli.serviceBase.Entity.GuliExcepiton;
import com.guli.ucenter.entity.UcenterMember;
import com.guli.ucenter.entity.vo.LoginCodeVO;
import com.guli.ucenter.entity.vo.LoginPasswordVO;
import com.guli.ucenter.entity.vo.MemberInfoVO;
import com.guli.ucenter.entity.vo.RegisterMemberVO;
import com.guli.ucenter.service.impl.UcenterMemberServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-13
 */
@Api(description = "会员信息管理模块")
@RestController
@RequestMapping("/edu/ucenterMember")
@ResponseBody
//@CrossOrigin // 解决跨域问题
public class UcenterMemberController {
    @Autowired
    private UcenterMemberServiceImpl memberService;

    @ApiOperation(value = "密码登录")
    @PostMapping("loginByPassword")
    public R loginByPassword(
            @ApiParam(name = "loginPasswordVO" , value = "手机号&&密码" ,required = true)
            @RequestBody LoginPasswordVO loginPasswordVO){
        String token = memberService.loginByPassword(loginPasswordVO);
        return R.ok().data("token" , token);
    }

    @ApiOperation(value = "验证码登录")
    @PostMapping("loginByCode")
    public R loginByCode(
            @ApiParam(name = "loginCodeVO" , value = "手机号&&验证码" ,required = true)
            @RequestBody LoginCodeVO loginCodeVO){
        String token = memberService.loginByCode(loginCodeVO);
        return R.ok().data("token" , token);
    }

    @ApiOperation(value = "注册用户")
    @PostMapping("registerMember")
    public R registeMember(
            @ApiParam(name = "registerMember" , value = "注册用户" ,required = true)
            @RequestBody RegisterMemberVO memberVO){
        boolean isSuccess = memberService.registerMember(memberVO);
        return R.ok().message("注册成功~~~");
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfoByToken(HttpServletRequest request){
        try {
            System.out.println("根据token获取登录用户信息");
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            MemberInfoVO memberInfoVO = memberService.getMemberInfo(memberId);
            return R.ok().data("memberInfo" , memberInfoVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliExcepiton(20001 , "未能够获取到用户信息！！！");
        }
    }

    @ApiOperation(value = "根据会员id，获取会员基本信息")
    @GetMapping("getMemberInfoByMemberId/{memberId}")
    public UcenterVO getMemberInfoByMemberId(
            @ApiParam(name = "memberId" , value = "会员id" , required = true)
            @PathVariable String memberId){
        UcenterMember member = memberService.getById(memberId);
        if(member == null){
            throw new GuliExcepiton(20001 , "当前会员信息不存在!!!");
        }
        UcenterVO ucenterVO = new UcenterVO();
        BeanUtils.copyProperties(member , ucenterVO);
        ucenterVO.setMemberId(member.getId());
        return ucenterVO;
    }

    @ApiOperation(value = "统计某天的会员注册人数")
    @GetMapping("countRegisterMembers/{day}")
    public Integer countRegisterMembers(
            @ApiParam(name = "day" , value = "日期" ,required = true)
            @PathVariable String day){
        Integer sum = memberService.countRegisterMembers(day);
        return sum;
    }
}

